<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/11
 * Time: 21:11
 */

namespace Xcx\Controller;

use Xcx\Common\ErrorCode;
use Xcx\Model\CartModel;
use Xcx\Model\GoodsBargainModel;

class CartController extends BaseController {

    /**
     * 添加商品至购物车
     */
    public function addCart()
    {
        $gid = I('gid');
        $sepcID = I('spec_id');
        $count = I('count');

        $Cart = new CartModel();

        $isInCart = $Cart->checkGoodsIsInCart($gid,$sepcID);
        if ($isInCart) {
            $res = $Cart->updateCart($gid, $sepcID, $count);
        } else {
            $res = $Cart->addCart($gid,$sepcID,$count);
        }

        if ($res) {
            //将用户数据返回到小程序
            $arr = array('code'=>0);
        } else {
            $arr = array('code'=>ErrorCode::$Fail,'msg'=>'购物车加入失败');
        }

        $this->ajaxReturn($arr);
    }

    /**
     * 获取购物车列表
     */
    public function getCartList()
    {
        $Cart = new CartModel();
        $list = $Cart->getCartList();
        $arr = array('code'=>0,'list'=>$list);
        $this->ajaxReturn($arr);
    }

    public function getCurrentBargain()
    {
        $Bargain = new GoodsBargainModel();
        $data = $Bargain->getCurrentBargain();
        $arr = array('code'=>0,'data'=>$data);
        $this->ajaxReturn($arr);
    }

    /**
     * 将商品移除购物车
     */
    public function removeCart()
    {
        $id =I('id');
        $Cart = new CartModel();
        $res = $Cart->removeCart($id);
        if ($res) $arr = array('code'=>0);
        else $arr = array('code'=>ErrorCode::$Fail,'list'=>'移除失败');
        $this->ajaxReturn($arr);

    }

    public function setIncCount()
    {
        $id =I('id');
        $Cart = new CartModel();
        $res = $Cart->setIncCount($id);
        if ($res) $arr = array('code'=>0);
        else $arr = array('code'=>ErrorCode::$Fail,'msg'=>'增加失败');
        $this->ajaxReturn($arr);
    }

    public function setDecCount()
    {
        $id =I('id');
        $Cart = new CartModel();
        $res = $Cart->setDecCount($id);
        if ($res) $arr = array('code'=>0);
        else $arr = array('code'=>ErrorCode::$Fail,'msg'=>'减少失败');
        $this->ajaxReturn($arr);
    }

}